package org.openea.eap.module.obpm.controller.form;

import org.openbpm.base.api.aop.annotion.CatchErr;
import org.openbpm.base.api.query.QueryFilter;
import org.openbpm.base.api.response.impl.ResultMsg;
import org.openbpm.base.core.id.IdUtil;
import org.openbpm.base.core.util.StringUtil;
import org.openbpm.base.db.model.page.PageResult;
import org.openbpm.base.rest.ControllerTools;
import org.openbpm.base.rest.util.RequestUtil;
import org.openea.eap.module.obpm.service.form.FormTemplateManager;
import org.openea.eap.module.obpm.model.form.FormTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


@RestController
@RequestMapping({"/form/formTemplate/"})
public class FormTemplateController
        extends ControllerTools {
    @Autowired
    FormTemplateManager formTemplateManager;

    @RequestMapping({"save"})
    @CatchErr(write2response = true, value = "保存自定义对话框失败")
    public ResultMsg<FormTemplate> save(HttpServletRequest request, HttpServletResponse response, @RequestBody FormTemplate formTemplate) throws Exception {
        if (StringUtil.isEmpty(formTemplate.getId())) {
            formTemplate.setEditable(true);
            formTemplate.setId(IdUtil.getSuid());
            this.formTemplateManager.create(formTemplate);
        } else {
            this.formTemplateManager.update(formTemplate);
        }
        return getSuccessResult(formTemplate, "保存自定义对话框成功");
    }


    @RequestMapping({"getObject"})
    @CatchErr(write2response = true, value = "获取formTemplate异常")
    public ResultMsg<FormTemplate> getObject(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String id = RequestUtil.getString(request, "id");
        String key = RequestUtil.getString(request, "key");
        FormTemplate formTemplate = null;
        if (StringUtil.isNotEmpty(id)) {
            formTemplate = (FormTemplate) this.formTemplateManager.get(id);
        } else if (StringUtil.isNotEmpty(key)) {
            formTemplate = this.formTemplateManager.getByKey(key);
        }

        return getSuccessResult(formTemplate);
    }


    @RequestMapping({"listJson"})
    @ResponseBody
    public PageResult listJson(HttpServletRequest request, HttpServletResponse response) throws Exception {
        QueryFilter queryFilter = getQueryFilter(request);
        List<FormTemplate> list = this.formTemplateManager.query(queryFilter);
        return new PageResult(list);
    }


    @RequestMapping({"remove"})
    @CatchErr(write2response = true, value = "删除表单模板失败")
    public ResultMsg<String> remove(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String[] aryIds = RequestUtil.getStringAryByStr(request, "id");
        this.formTemplateManager.removeByIds(aryIds);
        return getSuccessResult("删除表单模板成功");
    }


    @RequestMapping({"initTemplate"})
    @CatchErr(write2response = true, value = "初始化模板失败")
    public ResultMsg<String> initTemplate(HttpServletRequest request, HttpServletResponse response) throws Exception {
        this.formTemplateManager.initAllTemplate();
        return getSuccessResult("初始化模板成功");
    }
}
